﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Text.RegularExpressions;

namespace SocketAPI
{
    public class HeartBeatClient
    {
        private string ip;
        private int port;
        private Socket hbClient;
        private IPEndPoint remoteEP;
        private byte[] bytes = new byte[1024];
        public HeartBeatClient()
        {
            ip = "127.0.0.1";
            port = 5050;
        }
        public void runThread()
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = IPAddress.Parse(ip);
            remoteEP = new IPEndPoint(ipAddress,5050);
            hbClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SpecialConsole.Text.WriteLine("Heartbeat Client Trying to Start on " + remoteEP.Address.ToString() + ":" + remoteEP.Port + "...", ConsoleColor.Magenta);
            //Try and Connect
            try
            {
                hbClient.Connect(remoteEP);
            } catch (Exception e){}
            sendEndpoint();
            sendHeartBeat();
            sendAddress();
            sendPort();
            sendLocation();
            sendCurrentPlayers();
            sendMaxPlayers();
            sendType();
            sendOptional();
            sendOwner();
            // Release the socket.
            try
            {
                hbClient.Shutdown(SocketShutdown.Both);
                hbClient.Close();
            }
            catch (Exception e)
            {

            }
        }
        public void sendEndpoint()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendEndPoint->" + socketAPI.SocketServerConn.endPoint.Address.ToString() + ":" + socketAPI.SocketServerConn.endPoint.Port.ToString() + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server EndPoint!");
            }
            catch (Exception e)
            {

            }
        }
        public void reStartUp()
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = IPAddress.Parse(ip);
            remoteEP = new IPEndPoint(ipAddress, 5050);
            hbClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SpecialConsole.Text.WriteLine("Heartbeat Client Trying to Start on " + remoteEP.Address.ToString() + ":" + remoteEP.Port + "...", ConsoleColor.Magenta);
            //Try and Connect
            try
            {
                hbClient.Connect(remoteEP);
            }
            catch (Exception e) { }
            sendEndpoint();
        }
        public void reShutDown()
        {
            // Release the socket.
            try
            {
                hbClient.Shutdown(SocketShutdown.Both);
                hbClient.Close();
            }
            catch (Exception e)
            {

            }
        }
        public void sendOwner()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendOwner->" + socketAPI.SocketServerConn.serverSettings.Owner + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Owner Information!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendOptional()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendOptional->" + socketAPI.SocketServerConn.serverSettings.Optional + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Optional Information!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendType()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendType->" + socketAPI.SocketServerConn.serverSettings.Type + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Type!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendMaxPlayers()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendMaxPlayers->" + socketAPI.SocketServerConn.serverSettings.MaxPlayers + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Max Players!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendCurrentPlayers()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendCurrentPlayers->" + socketAPI.SocketServerConn.serverSettings.CurrentPlayers + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Current Players!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendLocation()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendLocation->New Zealand\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Location!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendPort()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendPort->" + socketAPI.SocketServerConn.endPoint.Port + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Port!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendAddress()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->List->SendAddress->" + socketAPI.SocketServerConn.endPoint.Address.ToString() + "\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);
                Console.WriteLine("Sent Server Address!");
            }
            catch (Exception e)
            {

            }
        }
        public void sendHeartBeat()
        {
            //Try and Connect
            try
            {
                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("MasterServer->Heartbeat->Send->Beat\n");

                // Send the data through the socket.
                int bytesSent = hbClient.Send(msg);

                // Receive the response from the remote device.
                int bytesRec = hbClient.Receive(bytes);
                string recv = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                string[] splitData = Regex.Split(recv, "\n");
                foreach (string line in splitData)
                {
                    string[] splitCommand = Regex.Split(line, "->");
                    try
                    {
                        if (line != "")
                        {
                            switch (splitCommand[0])
                            {
                                case "MasterServer":
                                    switch (splitCommand[1])
                                    {
                                        case "Heartbeat":
                                            switch (splitCommand[2])
                                            {
                                                case "Verify":
                                                    SpecialConsole.Text.WriteLine("Heartbeat Verified!", ConsoleColor.Magenta);
                                                    break;
                                            }
                                            break;
                                    }
                                    break;
                            }
                        }
                    }
                    catch (Exception e)
                    {

                    }
                }
            }
            catch (SocketException se)
            {
                SpecialConsole.Text.WriteLine("Error Starting Heartbeat Client!", ConsoleColor.Magenta);
            }
        }
    }
}
